﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace FSC.HaNamAPApplication
{
    public partial class HNAP_AdminAPReceice : System.Web.UI.UserControl
    {
        public string CurrentUrlWithoutParam
        {
            get
            {
                return Controler.UrlWithoutParam(Request.RawUrl);
            }
        }

        private int ProcessAPId
        {
            get
            {
                int ProcessAPId = -1;
                if (Request.QueryString["id"] != null && int.TryParse(Request.QueryString["id"].ToString(), out ProcessAPId))
                {
                    ProcessAPId = int.Parse(Request.QueryString["id"].ToString());
                }
                return ProcessAPId;
            }
        }

        private string modeform
        {
            get
            {
                string modeform = "a";
                if (Request.QueryString["m"] != null && Request.QueryString["m"].ToString() != "")
                {
                    modeform = Request.QueryString["m"].ToString();
                }
                return modeform;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                BindDropAP(false);
                BindDropOffice();
            }
        }

        private void LoadData()
        {
            txtReceivingDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
            txtNumberRecord.Text = "1";
        }

        private void BindDropAP(bool isSearch)
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);

            // Bind Fields
            var data = from a in db.AdminProcedures select a;

            if (isSearch)
            {
                if (ddlOffice.SelectedValue != "-1")
                {
                    data = from d in data 
                                where d.ExecuteOffice_ID == int.Parse(ddlOffice.SelectedValue)
                                select d;
                }
            }

            data = data.OrderBy(a => a.Name);

            ddlAPName.DataValueField = "AP_ID";
            ddlAPName.DataTextField = "Name";
            ddlAPName.DataSource = data;
            ddlAPName.DataBind();

            ddlAPName.Items.Insert(0, new ListItem("--Chọn TTHC--", "-1"));
        }

        private void BindDropOffice()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);

            // Bind Offices
            var dataOffice = from o in db.Offices
                             orderby o.Name
                             select new
                             {
                                 o.Office_ID,
                                 o.Name
                             };
            ddlOffice.DataValueField = "Office_ID";
            ddlOffice.DataTextField = "Name";
            ddlOffice.DataSource = dataOffice;
            ddlOffice.DataBind();

            ddlOffice.Items.Insert(0, new ListItem("--Lọc theo cơ quan--", "-1"));
        }

        private void UpdateData()
        {
            //HaNamAPDataDataContext db = new HaNamAPDataDataContext();
            //ProcessingAP objProcessAP;

            //if (this.modeform.Equals("u"))
            //    objProcessAP = db.ProcessingAPs.SingleOrDefault(a => a.ProcessingAP_ID == this.ProcessAPId);
            //else
            //    objProcessAP = new ProcessingAP();

            //if (objProcessAP == null) return;

            //objProcessAP.AP_ID = int.Parse(ddlAPName.SelectedValue);
            //objProcessAP.ReceivingDate = DateTime.Parse(txtReceivingDate.Text);
            //objProcessAP.ReceivingComponentRecord = txtReceivingRecordComponent.Text;
            //objProcessAP.NumberOfRecords = int.Parse(txtNumberRecord.Text);
            //objProcessAP.AppointmentResultDate = DateTime.Parse(txtAppointmentResultDate.Text);

            //objProcessAP.PopularUser = txtName.Text;
            ////objProcessAP.PassportID = Convert.ToChar(txtPassportId.Text);
            //objProcessAP.Address = txtAddress.Text;
            ////objProcessAP.Phone = Convert.ToChar(txtPhone.Text);
            //objProcessAP.Email = txtEmail.Text;


            

            //if (this.modeform.Equals("a"))
            //{
            //    db.ProcessingAPs.InsertOnSubmit(objProcessAP);
            //}

            //db.SubmitChanges();
            Response.Redirect(CurrentUrlWithoutParam + "?page=apresult");
        }

        protected void ddlOffice_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDropAP(true);
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            UpdateData();
        }
    }
}